import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class java01 {
    public static void main(String[] args) throws Exception {
        String strXingURL = "https://hanyu.baidu.com/shici/detail?from=kg1&highlight=&pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&srcid=51369";
        String manMingXingURL = "http://www.haoming8.cn/mingzi/59965.html";
        String womanMingXingURL = "http://www.haoming8.cn/mingzi/53426.html";

        String xingContet = webCarawler(strXingURL);

        String manmingContet = webCarawler(manMingXingURL);

        String womanmingContet = webCarawler(womanMingXingURL);

        ArrayList<String> listXing = getDataXing(xingContet, "(.{4})(，|。)");

        ArrayList<String> manlistMing = getDataMing(manmingContet, "(..、){4}..");

        ArrayList<String> womanlistMing = getDataMing(womanmingContet, "(..、){4}..");

        ArrayList<String> xing = execDataXing(listXing);
        System.out.println(xing);

        ArrayList<String> manming = execDataMing(manlistMing);
        System.out.println(manming);

        ArrayList<String> womanming = execDataMing(womanlistMing);
        System.out.println(womanming);

        ArrayList<String> manxingMing = getInfo(xing, manming, 50, "男");  // 固定为男性
        System.out.println("男生姓名:" + manxingMing);
        ArrayList<String> womanxingMing = getInfo(xing, womanming, 50, "女");  // 固定为女性
        System.out.println("女生姓名" + womanxingMing);

        FileWriter fw = new FileWriter("names.txt");
        for (String string : manxingMing) {
            fw.write(string + "\n");
            fw.flush();
        }
        for (String string : womanxingMing) {
            fw.write(string + "\n");
            fw.flush();
        }
        fw.close();
    }

    private static ArrayList<String> getInfo(ArrayList<String> xing, ArrayList<String> ming, int count, String gender) {
        HashSet<String> set = new HashSet<>();
        Random random = new Random();
        while (true) {
            if (set.size() == count) {
                break;
            }
            Collections.shuffle(xing);
            Collections.shuffle(ming);
            int age = random.nextInt(7) + 18;  // 生成 18 到 24 之间的随机年龄
            set.add(xing.get(0) + ming.get(0) + "-" + gender + "-" + age);
        }
        return new ArrayList(set);
    }

    private static ArrayList<String> execDataMing(ArrayList<String> listMing) {
        ArrayList<String> ming = new ArrayList<>();
        for (String string : listMing) {
            String[] split = string.split("、");
            for (String string2 : split) {
                ming.add(string2);
            }
        }
        return ming;
    }

    private static ArrayList<String> execDataXing(ArrayList<String> listXing) {
        ArrayList<String> xing = new ArrayList<>();
        for (String string : listXing) {
            for (int i = 0; i < string.length() - 1; i++) {
                char charAt = string.charAt(i);
                xing.add(charAt + "");
            }
        }
        return xing;
    }

    private static ArrayList<String> getDataMing(String xingContet, String regex) {
        ArrayList<String> list = new ArrayList<>();

        Pattern pattern = Pattern.compile(regex);

        Matcher matcher = pattern.matcher(xingContet);
        while (matcher.find()) {
            String group = matcher.group();
            list.add(group);
        }
        return list;
    }

    private static ArrayList<String> getDataXing(String xingContet, String regex) {
        ArrayList<String> list = new ArrayList<>();

        Pattern pattern = Pattern.compile(regex);

        Matcher matcher = pattern.matcher(xingContet);
        while (matcher.find()) {
            String group = matcher.group();
            if (!group.contains("em")) {
                list.add(group);
            }
        }
        return list;
    }

    private static String webCarawler(String strXingURL) throws Exception {
        StringBuilder sb = new StringBuilder();

        URL url = new URL(strXingURL);

        URLConnection openConnection = url.openConnection();
        InputStream inputStream = openConnection.getInputStream();

        InputStreamReader isr = new InputStreamReader(inputStream, "utf-8");
        int ch;
        while ((ch = isr.read())!= -1) {
            sb.append((char) ch);
        }
        isr.close();
        return sb.toString();
    }
}